﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json;
using S.Archives.Domain.S.ArchivesDto;
using S.Common.Enums.Archives;
using S.Common.helper;
using System.Data;
using System.Security.Principal;
using static System.Net.WebRequestMethods;

namespace S.Archives.Read.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    
    public class HttpCilentController : ControllerBase
    {
        private readonly IConfiguration configuration;
        public HttpCilentController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        /// <summary>
        /// 跨服务请求角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetPageRole")]
        public IActionResult GetPageRole(int page = 1, int size = 5, string? name = "")
        {
            var jsonString = HttpClientHelper.GetResponse($"https://localhost:7109/api/RoleMenuRead/GetPageRole?pageIndex={page}&pageSize={size}&roleName={name}");

            // 反序列化并显示分页数据
            var response = JsonConvert.DeserializeObject<ResponseDto>(jsonString);

            return Ok(response);
        }

        public class ResponseDto
        {
            public List<RoleAddDto> RoleList { get; set; }
            public int PageCount { get; set; }
            public int TotalCount { get; set; }
        }
        /// <summary>
        /// 跨服务查询耳标下拉框
        /// </summary>
        /// <param name="EarTagId"></param>
        /// <returns></returns>
        /// 
        [HttpGet("GetEraTagState")]
        public IActionResult GetEraTagState(int MarkId)
        {
            
            var result= HttpClientHelper.GetResponse($"http://localhost:5081/api/Mark/GetMarkList?MarkId={MarkId}");
            var response = JsonConvert.DeserializeObject<List<MarkModel>>(result);
            return Ok(response);
        }
        public class MarkModel
        {
            /// <summary>
            /// 主键
            /// </summary>
          
            public int MarkId { get; set; }

            /// <summary>
            /// 耳标下拉框
            /// </summary>
          
            public string? EarTag { get; set; }

            /// <summary>
            /// 当前电子耳号
            /// </summary>
           
            public string? EITag { get; set; }
            /// <summary>
            /// 新电子耳号
            /// </summary>
          
            public string? NewEITag { get; set; }

           

            /// <summary>
            /// 戴标状态
            /// </summary>
           
            public string? MarkState { get; set; }

            /// <summary>
            /// 创建人
            /// </summary>
           
            public string? CreateBy { get; set; }

            /// <summary>
            /// 创建时间
            /// </summary>
          
            public DateTimeOffset CreateDate { get; set; }


            /// <summary>
            /// 修改人
            /// </summary>
          
            public string UpdateBy { get; set; }

            /// <summary>
            /// 修改时间
            /// </summary>
          
            public string? UpdateDate { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
          
            public string? UseState { get; set; }
        }
        /// <summary>
        /// 转舍记录
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet("TransferRecord")]
        public IActionResult TransferRecord(string EITag)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var result = HttpClientHelper.GetResponse(Gateway+$"/transferset/api/SheepFrom/GetSheepFrom?EarTag={EITag}");
            var response = JsonConvert.DeserializeObject<List<SheepFromDto>>(result);
            var data = response.SelectMany(x => new[]
            {
                new {x.SheepRescissionId,Reason="转舍", Content="转出栋舍:"+x.TurnOuBuildingManagementName+"转出栏位:"+x.TurnOuColumnManagementName+"转入栋舍:"+x.ShiftToBuildingManagementName+"转入栏位:"+x.ShiftToColumnManagementName,x.SurrenderDate,x.DocumentNumbe}
            }).ToList();
            return Ok(data);

        }
        public class SheepFromDto
        {
            public int SheepRescissionId { get; set; }
            /// <summary>
            /// 转出栋舍
            /// </summary>
            public string TurnOuBuildingManagementName { get; set; }
            /// <summary>
            /// 转出栏位
            /// </summary>
            public string TurnOuColumnManagementName { get; set; }
            /// <summary>
            /// 转入栋舍
            /// </summary>
            public string ShiftToBuildingManagementName { get; set; }
            /// <summary>
            /// 转入栏位
            /// </summary>
            public string ShiftToColumnManagementName { get; set; }
            /// <summary>
            /// 转舍日期
            /// </summary>
            public DateTime SurrenderDate { get; set; }
            /// <summary>
            /// 单据号
            /// </summary>
            public long DocumentNumbe { get; set; }
            /// <summary>
            /// 转舍原因
            /// </summary>
            public string Reason { get; set; }
            /// <summary>
            /// 转舍原因
            /// </summary>
            public string Content { get; set; }
        }

    }
}
